$(function () {
    var _ele = $('#ele-comment_bx').val();
    $('body').on('click', '#ppCommentBtn', function () {
        var courseId = $("#buyCourseId").val();
        var catalogId = $("#playId").val();
        checkComment(courseId,catalogId,commentFn);
    });

    /**
     * 评价前验证
     * @param courseId 课程ID
     * @param catalogId 目录ID
     * @param callback 成功后回调函数
     */
    function checkComment(courseId,catalogId,callback) {
        var params = {
            'courseId': courseId,
            'catalogId': catalogId
        };
        params = JSON.stringify(params);
        $.ajax({
            url: baseloaction + '/web/uc/course/comment/checkCourseCommentBefore',
            data: params,
            dataType: 'json',
            type: 'post',
            contentType: 'application/json;charset=utf-8',
            success: function (result) {
                if(result.success){
                    if($.isFunction(callback)){
                        callback();
                    }
                }else {
                    icModale(0, '', result.message);
                }
            },
            error:function () {
                icModale(0, '','响应不成功');
            }
        });
    }

    //评价与反馈
    function commentFn() {
        layer.open({
            type: 1,
            title: false,
            area: ['620px', '650px'],
            closeBtn: 0,
            btn: ['提交', '取消'],
            shadeClose: true,
            skin: 'lay_ppComment__wrap',
            content: _ele,
            yes: function (index, layero) {
                var params = {};
                $(".ionRangeSlider__wrap").each(function (index, element) {
                    var param = $(element).data('comment');
                    params[param] = parseInt($(element).data('from')) + 1;
                });
                params['comment'] = $('div[data-comment="comment"]').html();
                saveComment(params);
            }
        });

        //评价滑块
        $(".ionRangeSlider__wrap").ionRangeSlider({
            type: "single",
            from: 2,
            values: ["非常差", "差", "正常", "好", "非常好"]
        });
    }


    /**
     * 保存评论
     */
    function saveComment(params) {
        params['courseId'] = $("#buyCourseId").val();
        params['catalogId'] = $("#playId").val();
        params = JSON.stringify(params);
        $.ajax({
            url: baseloaction + '/web/uc/course/comment/saveCourseComment',
            type:'post',
            data: params,
            dataType: 'json',
            contentType:'application/json;charset=utf-8',
            success: function (response) {
                if (response.success==true) {
                    layer.closeAll();
                }
                icModale(0, '', response.message);
            },
            error: function () {
                icModale(0, '', '系统繁忙，请稍候再试');
            }
        });
    }
});

